import 'package:flutter/material.dart';
import 'package:x3_component_core/components/base_components/base_drop_down_icon.dart';
import 'package:x3_component_core/controller/base_bottom_sheet_controller.dart';
import 'package:x3_component_core/x3_component_core.dart';

/// 底部弹出选择器
class BaseBottomSheetSelector extends StatefulWidget {
  /// 控制器
  final BaseBottomSheetController controller;

  /// 改变事件
  final Function(BaseBottomSheetSelectorModel) onChange;

  /// 是否显示 icon
  final bool isIcon;

  /// 是否禁用
  final bool disable;

  /// 内边距
  final EdgeInsets padding;

  /// 装饰item
  final Widget Function(BaseBottomSheetSelectorModel, bool) decorator;

  const BaseBottomSheetSelector({
    Key key,
    @required this.onChange,
    @required this.controller,
    this.isIcon = true,
    this.disable = false,
    this.padding = EdgeInsets.zero,
    this.decorator,
  })  : assert(controller != null),
        assert(onChange != null),
        super(key: key);
  @override
  _BaseBottomSheetSelectorState createState() => _BaseBottomSheetSelectorState();
}

class _BaseBottomSheetSelectorState extends State<BaseBottomSheetSelector> {
  /// 数据列
  List<BaseBottomSheetSelectorModel> get _items => widget.controller.items;

  /// 提示语
  String get _hit => widget.controller?.hit??'';

  /// 默认选中值
  BaseBottomSheetSelectorModel get _selectedItem => widget.controller.selectedItem;

  @override
  void initState() {
    widget.controller.addListener(() {
      _onShowModalBottomSheet();
    });
    super.initState();
  }

  @override
  void dispose() {
    widget.controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AbsorbPointer(
      absorbing: widget.disable,
      child: InkWell(
        onTap: _items.length == 0 ? null : _onShowModalBottomSheet,
        child: Container(
          padding: widget.padding,
          child: Row(
            children: [
              Expanded(child: Container(alignment: Alignment.centerLeft, child: _buildText())),
              widget.isIcon == true ? BaseDropDownIcon() : SizedBox(),
            ],
          ),
        ),
      ),
    );
  }

  /// 文本显示
  Widget _buildText() {
    if (_selectedItem.value == null || _selectedItem.value == '请选择' ) {
      return Text(_hit, style: Theme.of(context).inputDecorationTheme.hintStyle);
    }
    return Text(_selectedItem.value, style: Theme.of(context).textTheme.bodyText2);
  }

  /// 下方弹出框
  void _onShowModalBottomSheet() {
    BaseComponentUtils.buildDefaultBottomSheet(
      context,
      hit: _hit,
      child: ListView.builder(
        itemBuilder: _buildItem,
        itemExtent: 49,
        itemCount: _items.length,
      ),
    );
  }

  /// 创建每行 item
  Widget _buildItem(BuildContext context, int index) {
    BaseBottomSheetSelectorModel item = _items[index];
    bool isSelected = widget.controller.isSelected(item);
    return InkWell(
      child: _buildItemContent(item, isSelected),
      onTap: () {
        Navigator.of(context, rootNavigator: true).pop();
        if (!isSelected) {
          setState(() {
            widget.controller.selectedItem = item;
            widget.onChange(item);
          });
        }
      },
    );
  }

  /// 创建item
  Widget _buildItemContent(BaseBottomSheetSelectorModel item, bool isSelected) {
    return Container(
      height: 49,
      child: Column(
        children: [
          widget.decorator != null ? widget.decorator(item, isSelected) : _buildDefaultItem(item, isSelected),
          BaseComponentUtils.buildHorizontalDivider(),
        ],
      ),
    );
  }

  /// 默认模板
  Widget _buildDefaultItem(BaseBottomSheetSelectorModel item, bool isSelected) {
    return Container(
      color: isSelected == true ? Color(0xFFF3F3F3) : Colors.transparent,
      alignment: Alignment.center,
      height: 48,
      child: Text(item.value ?? '', style: Theme.of(context).textTheme.bodyText2),
    );
  }
}
